const GroupBy = require("../template")

function testGroupByAdvancedMethods() {
  console.log(
    "1. 数组分组高级方法 - 函数分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result).length === 2 && result['even'].length === 5 && result['odd'].length === 5
    })()
  )

  console.log(
    "2. 数组分组高级方法 - 深度分组测试:",
    (() => {
      const array = [
        { user: { name: 'Alice', age: 25 }, city: 'New York' },
        { user: { name: 'Bob', age: 30 }, city: 'London' },
        { user: { name: 'Charlie', age: 25 }, city: 'Paris' }
      ]
      const result = GroupBy.groupByDeep(array, 'user.age', 2)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "3. 数组分组高级方法 - 多条件分组测试:",
    (() => {
      const array = [
        { name: 'Alice', age: 25, city: 'New York' },
        { name: 'Bob', age: 30, city: 'London' },
        { name: 'Charlie', age: 25, city: 'Paris' },
        { name: 'David', age: 30, city: 'Tokyo' }
      ]
      const result = GroupBy.groupByMultiple(array, 'age', 'city')
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "4. 数组分组高级方法 - 条件过滤分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithFilter(array, x => x % 2 === 0 ? 'even' : 'odd', x => x > 5)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "5. 数组分组高级方法 - 映射分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithMap(array, x => x % 2 === 0 ? 'even' : 'odd', x => x * 2)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "6. 数组分组高级方法 - 排序分组测试:",
    (() => {
      const array = [3, 1, 2, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithSort(array, x => x % 2 === 0 ? 'even' : 'odd', (a, b) => a - b)
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "7. 数组分组高级方法 - 统计分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithStats(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result.result).length === 2 && typeof result.stats === 'object'
    })()
  )

  console.log(
    "8. 数组分组高级方法 - 转换分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithTransform(array, x => x % 2 === 0 ? 'even' : 'odd', {
        even: arr => arr.map(x => x * 2),
        odd: arr => arr.map(x => x * 3)
      })
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "9. 数组分组高级方法 - 验证分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithValidation(array, x => x % 2 === 0 ? 'even' : 'odd', { min: 1, max: 10 })
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "10. 数组分组高级方法 - 空数组分组测试:",
    (() => {
      const array = []
      const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result).length === 0
    })()
  )

  console.log(
    "11. 数组分组高级方法 - 单元素数组分组测试:",
    (() => {
      const array = [1]
      const result = GroupBy.groupBy(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result).length === 1 && result['odd'].length === 1
    })()
  )

  console.log(
    "12. 数组分组高级方法 - 复杂对象分组测试:",
    (() => {
      const array = [
        { id: 1, category: 'A', value: 100 },
        { id: 2, category: 'B', value: 200 },
        { id: 3, category: 'A', value: 300 },
        { id: 4, category: 'B', value: 400 }
      ]
      const result = GroupBy.groupBy(array, 'category')
      return Object.keys(result).length === 2 && result['A'].length === 2 && result['B'].length === 2
    })()
  )

  console.log(
    "13. 数组分组高级方法 - 嵌套属性分组测试:",
    (() => {
      const array = [
        { user: { profile: { level: 'beginner' } } },
        { user: { profile: { level: 'intermediate' } } },
        { user: { profile: { level: 'beginner' } } },
        { user: { profile: { level: 'advanced' } } }
      ]
      const result = GroupBy.groupBy(array, 'user.profile.level')
      return Object.keys(result).length === 3
    })()
  )

  console.log(
    "14. 数组分组高级方法 - 函数返回值分组测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupBy(array, x => {
        if (x <= 3) return 'small'
        if (x <= 6) return 'medium'
        return 'large'
      })
      return Object.keys(result).length === 3 && result['small'].length === 3
    })()
  )

  console.log(
    "15. 数组分组高级方法 - 混合类型分组测试:",
    (() => {
      const array = [1, '1', 2, '2', 3, '3']
      const result = GroupBy.groupBy(array, x => typeof x)
      return Object.keys(result).length === 2 && result['number'].length === 3 && result['string'].length === 3
    })()
  )
}

testGroupByAdvancedMethods()
